package com.chaobl.goods.sku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaobl.goods.catalog.entity.Catalog;
import com.chaobl.goods.catalog.service.ICatalogService;
import com.chaobl.goods.shop.service.RedisService;
import com.chaobl.goods.sku.dto.SkuEx;
import com.chaobl.goods.sku.entity.Sku;
import com.chaobl.goods.sku.service.ISkuService;
import com.chaobl.goods.spu.entity.Spu;
import com.chaobl.goods.spu.service.ISpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店商品详细信息 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-04-19
 */
@RestController
@RequestMapping("/sku/sku")
public class SkuController {
    @Autowired
    RedisService redisService;

    @Autowired
    ISkuService skuService;

    @Autowired
    ICatalogService catalogService;

    // 首页10个分类，每个展示8个商品
    @GetMapping("/listByCatalogId")
    public Object listByCatalogId(Integer enterpriseId, Integer shopId, Integer catalogId) {
        String key = "/sku/sku/listByCatalogId" + enterpriseId + "_" + shopId + "_" + catalogId;
        Object obj = redisService.get(key);
        if(obj != null)
            return obj;

        List listOut = new ArrayList();

        IPage<Catalog> page = catalogService.page(new Page<Catalog>().setSize(10));
        for (Catalog catalog: page.getRecords()
             ) {
            Map<String, Object> map = new HashMap();
            if(catalog.getId().equals(9))
                System.out.println();
            List<SkuEx> list = skuService.listByCatalogId(enterpriseId, shopId, catalog.getId());
            List<Map<String, Object>> list2 = new ArrayList<>();

            if(list != null && list.size() > 0) {
                map.put("catalogName", list.get(0).getCatalogName());
                map.put("catalogHeadImg", list.get(0).getCatalogHeadImg());
                map.put("list", list);
            }

            listOut.add(map);
        }

        if (listOut != null)
            redisService.set(key, listOut);

        return listOut;
    }
}
